# -*- coding: utf-8 -*- 
# 根据数据集图片生成路径索引文件：
# ImageSets/Main/
#             |—— train.txt
#             |—— val.txt
# scripts/
#       |—— 2007_train.txt
#       |—— 2007_val.txt
#       |—— train.txt
#       |—— train.all.txt
import os

sets = [('2007', 'train'), ('2007', 'val')]
wd = os.getcwd()

for year, image_set in sets:
    # 获取所有文件名存入列表
    file_list = os.listdir('VOCdevkit/VOC%s/JPEGImages/%s' %(year, image_set)) 
    image_ids_file = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt' %(year, image_set), 'w')
    list_file = open('%s_%s.txt' % (year, image_set), 'w')
    for file_name in file_list:
        # 将相应image_set的文件名写入txt
        image_ids_file.write(os.path.splitext(file_name)[0] + '\n')
        # 写入图片路径
        list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s/%s.jpg\n' %(wd, year, image_set, os.path.splitext(file_name)[0]))
    image_ids_file.close()
    list_file.close()

# 执行命令cat，并重定向
os.system("cat 2007_train.txt 2007_val.txt > train.txt")
os.system("cat 2007_train.txt 2007_val.txt > train.all.txt")